package com.xsz.oa.service.impl;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xsz.oa.dao.RoleDao;
import com.xsz.oa.dao.UserDao;
import com.xsz.oa.model.entity.sys.Role;
import com.xsz.oa.model.entity.sys.User;
import com.xsz.oa.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService{
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserDao userDao;

	public Role findById(Integer id) {
		return roleDao.findById(id);
	}

	/**
	 * 根据角色id查找角色
	 */
	public Set<String> findRoles(Integer... roleIds) {
		Set<String> roles = new HashSet<String>();
		for (Integer roleId : roleIds) {
			Role role = findById(roleId);
			roles.add(role.getName());
		}
		return roles;
	}

	/**
	 * 根据用户名查找角色
	 */
	public Set<String> findRoles(String username) {
		
		User user = userDao.findByUsername(username);
		return findRoles(user.getRoleIds().toArray(new Integer[0]));
	}

}
